<?php
	$groupid = checkUserGroup($_SESSION['username']);
	$members = getMembers($groupid);
	$totalmembers = getNumUsers($groupid);
	$currentmember = 0;
	$billshare = 0;
	$error = 0;

	// code to create bill details
	$billid = createBill($_POST['billName'], $_POST['billType'], $_POST['billTotal'], $_POST['dueDate']);
	
	// code to add bill shares to database
	if ($billid > 0) {
		while ($row = mysql_fetch_array($members)) {
			$currentmember = $currentmember + 1;
			switch($currentmember) {
				case 1:
					$billshare = $_POST['member1'];
					break;
				case 2:
					$billshare = $_POST['member2'];
					break;
				case 3:
					$billshare = $_POST['member3'];
					break;
				case 4:
					$billshare = $_POST['member4'];
					break;
				case 5:
					$billshare = $_POST['member5'];
					break;
				case 6:
					$billshare = $_POST['member6'];
					break;
				case 7:
					$billshare = $_POST['member7'];
					break;
				case 8:
					$billshare = $_POST['member8'];
					break;
				default:
					$billshare = 0;
			}
			if ($billshare != "" && $billshare > 0) {
				if (!addShare($billid, $row[0], $billshare)) {
					$error = 1;
				}
			}
		}
		if ($error == 1) {
			echo '<p>There was an error processing the bill details. Please contact an administrator.</p>';
		}
		else {
			echo '<p>The bill and share details have been successfully added to the database!</p>';
		}
	}
	else {
		echo '<p>There was an error processing the bill creation event. Please contact the administrator.</p>';
	}
	
	
?>